<template>
  <div class="manage">
    <el-dialog
      :title="operateType === 'add' ? '新增用户' : '更新用户'"
      :visible.sync="isShow"
      :top="'margin-right:0'"
    >
      <common-form
        :formLabel="operateFormLabel"
        :form="operateForm"
      ></common-form>
      <div
        slot="footer"
        class="dialog-footer"
        style="position: absolute;bottom: 10px;right: 20px;"
      >
        <el-button @click="isShow = false">取 消</el-button>
        <el-button type="primary" @click="confirm">确 定</el-button>
      </div>
    </el-dialog>
    <div class="manage-header">
      <el-button type="primary" @click="add">+ 新增</el-button>
      <common-form inline :formLabel="formLabel" :form="searchForm">
        <el-button type="primary" @click="getList">搜索</el-button>
      </common-form>
    </div>
    <common-table
      :tableData="tableData"
      :tableLabel="tableLabel"
      :config="config"
      @changePage="getList"
      @edit="editUser"
      @delete="delUser"
    ></common-table>
  </div>
</template>

<script>
import CommonForm from "@/components/CommonForm";
import CommonTable from "@/components/CommonTable";
export default {
  components: {
    CommonForm,
    CommonTable
  },
  data() {
    return {
      operateType: "add",
      isShow: false,
      tableData: [],
      tableLabel: [
        {
          prop: "name",
          label: "姓名"
        },
        {
          prop: "age",
          label: "年龄"
        },
        {
          prop: "sexLabel",
          label: "性别"
        },
        {
          prop: "birth",
          label: "出生日期",
          width: 200
        },
        {
          prop: "addr",
          label: "地址",
          width: 320
        }
      ],
      config: {
        page: 1,
        total: 30,
        loading: false
      },
      confirmLoading: false,
      operateForm: {
        id: "",
        name: "",
        addr: "",
        age: "",
        birth: "",
        sex: ""
      },
      operateFormLabel: [
        {
          model: "id",
          label: "",
          type: "hidden"
        },
        {
          model: "name",
          label: "姓名"
        },
        {
          model: "age",
          label: "年龄"
        },
        {
          model: "sex",
          label: "性别",
          type: "select",
          opts: [
            {
              label: "男",
              value: 1
            },
            {
              label: "女",
              value: 0
            }
          ]
        },
        {
          model: "birth",
          label: "出生日期",
          type: "date",
          format: "yyyy-MM-dd"
        },
        {
          model: "addr",
          label: "地址"
        }
      ],
      searchForm: {
        name: ""
      },
      formLabel: [
        {
          model: "name",
          label: ""
        }
      ]
    };
  },
  methods: {
    add() {
      this.operateForm = {};
      this.operateType = "add";
      this.isShow = true;
    },
    getList() {
      this.config.loading = true;
      this.$http
        .get("/user/getUser", {
          params: {
            ...this.searchForm,
            page: this.config.page
          }
        })
        .then(res => {
          this.tableData = res.data.list.map(item => {
            item.sexLabel = item.sex === 0 ? "女" : "男";
            return item;
          });
          this.config.total = res.data.count;
          this.config.loading = false;
        });
    },
    editUser(row) {
      this.operateType = "edit";
      this.isShow = true;
      this.operateForm = row;
    },
    openLoading(){
      document.getElementById("ajaxLoading").style.display = "block";
    },
    closeLoading(){
      document.getElementById("ajaxLoading").style.display = "none";
    },
    confirm() {
      this.openLoading();
      if (this.operateType === "edit") {
        this.$http
          .post("/user/edit", this.operateForm)
          .then(res => {
            this.closeLoading();
            console.log(res.data, "修改数据");
            this.isShow = false;
            this.getList();
          })
          .catch(error => {
            this.closeLoading();
            console.log(error);
            this.isShow = false;
          });
      } else if (this.operateType === "add") {
        this.$http
          .post("/user/createUser", this.operateForm)
          .then(res => {
            this.closeLoading();
            console.log(res);
            this.isShow = false;
            this.getList();
          })
          .catch(error => {
            this.closeLoading();
            console.log(error);
            this.isShow = false;
          });
      }
    },
    delUser(row) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          let id = row.id;
          this.$http
            .get("/user/del", {
              params: {
                id
              }
            })
            .then(res => {
              console.log(res.data);
              this.$message({
                type: "success",
                message: "删除成功!"
              });
              this.getList();
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    }
  },
  created() {
    this.getList();
  }
};
</script>

<style lang="scss">
.manage {
  height: 90vh;
  padding-bottom: 20px;
  overflow: hidden;
  &-header {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
  }
  .el-dialog {
    height: 100%;
    margin: 0 0 0 50%;
  }
}
</style>
